﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CopySniffer.Search;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Xml;

namespace ResultSerialization
{
    class Program
    {
        static void Main(string[] args)
        {
            List<PairedSearchResults> list = new List<PairedSearchResults>();
            for (int i = 0; i < 5; ++i)
            {
                SearchInfo newInfo = new SearchInfo(new string[] { "TestA" + i.ToString(), "TestB" + i.ToString() });

                List<SearchResult> results = new List<SearchResult>();
                for (int j = 0; j < 3; ++j)
                {
                    string suffix = i.ToString() + "-" + j.ToString();
                    results.Add(new SearchResult() 
                        { 
                            Abstract = "Abstract" + suffix,
                            Date = DateTime.Now,
                            Title = "Title" + suffix,
                            Url = "url" + suffix
                        });
                }

                PairedSearchResults pair = new PairedSearchResults(newInfo, results);
                list.Add(pair);
            }

            //var list2 = list.ToArray();
            //XmlSerializer serializer = new XmlSerializer(list2.GetType());
            //serializer.Serialize(Console.Out, list);
            DataContractSerializer serializer = new DataContractSerializer(list.GetType());
            StringBuilder builder = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(builder,
                new XmlWriterSettings()
                {
                    Indent = true,
                    Encoding = Encoding.UTF8
                }
                );
            writer.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"PairedResultsStyleSheet.xslt\"");
            serializer.WriteObject(writer, list);
            writer.Flush();
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.LoadXml(builder.ToString());
            Console.WriteLine(builder.ToString());
            Console.ReadKey();
            Console.WriteLine(doc.OuterXml);
            Console.ReadKey();
        }
    }
}
